package com.wordpress.minhnvt1.sample;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainAppActivity extends ListActivity {
	
	protected EditText searchText;
	protected SQLiteDatabase db;
	protected Cursor cursor;
	protected ListAdapter listAdapter;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        db = (new DatabaseHelper(this)).getWritableDatabase();
        
        searchText = (EditText) findViewById(R.id.searchText);
       
    }
    
    public void search(View v) {
    	cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?", 
                                                new String[]{"%" + searchText.getText().toString() + "%"});
    	listAdapter = new SimpleCursorAdapter(this, 
    											R.layout.employee_list_item, 
    											cursor, 
    											new String[] {"firstName", "lastName", "title"}, 
    											new int[] {R.id.firstName, R.id.lastName, R.id.title});
    	setListAdapter(listAdapter);
    }
    
    public void onListItemClick(ListView parent, View view, int position, long id) {
    	Intent intent = new Intent(this, EmployeeDetails.class);
    	Cursor cursor = (Cursor) listAdapter.getItem(position);
    	intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
    	startActivity(intent);
    }
}